/*
 * Copyright 2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.fsquare.controller.account;

import org.broadleafcommerce.common.exception.ServiceException;
import org.broadleafcommerce.core.web.controller.account.BroadleafRegisterController;
import org.broadleafcommerce.profile.core.domain.Customer;
import org.broadleafcommerce.profile.web.core.CustomerState;
import org.broadleafcommerce.profile.web.core.form.RegisterCustomerForm;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.method.support.ModelAndViewContainer;

import com.fsquare.component.captcha.ReCaptchaProcessor;
import com.fsquare.controller.account.social.AuthSuccessController;
import com.fsquare.domain.social.SocialToken;
import com.fsquare.service.social.SocialService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * The controller responsible for registering a customer
 */
@Controller
@RequestMapping("/register")
public class RegisterController extends BroadleafRegisterController {
    
	@Resource(name="blReCaptchaProcessor")
    protected ReCaptchaProcessor reCaptchaProcessor;
	
	@Resource(name="socialService")
    protected SocialService socialService;
	
    @RequestMapping(method=RequestMethod.GET)
    public String register(HttpServletRequest request, HttpServletResponse response, Model model,
            @ModelAttribute("customRegisterCustomerForm") CustomRegisterCustomerForm customRegisterCustomerForm) {
        return super.register((RegisterCustomerForm)customRegisterCustomerForm, request, response, model);
    }
    
    @RequestMapping(method=RequestMethod.POST)
    public String processRegister(HttpServletRequest request, HttpServletResponse response, Model model,
            @ModelAttribute("customRegisterCustomerForm") CustomRegisterCustomerForm customRegisterCustomerForm, BindingResult errors) throws ServiceException {
    	reCaptchaProcessor.test(request, errors);
    	String returnUrl = super.processRegister((RegisterCustomerForm)customRegisterCustomerForm, errors, request, response, model);
    	Customer customer = CustomerState.getCustomer();
    	if(!customer.isAnonymous()){
	    	SocialToken socialToken = customRegisterCustomerForm.getSocialToken();
	    	if(socialToken != null){
	    		socialToken.setCustomer(customer);
	    		socialService.saveSocialToken(socialToken);
	    	}
    	}
    	return returnUrl;
    }
    
    @ModelAttribute("customRegisterCustomerForm") 
    public RegisterCustomerForm initCustomerRegistrationForm() {
    	RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
    	
    	CustomRegisterCustomerForm customRegisterCustomerForm = (CustomRegisterCustomerForm)requestAttributes.getAttribute(AuthSuccessController.PREFILLED_CUSTOM_REGISTER_CUSTOMER_FORM, RequestAttributes.SCOPE_SESSION);
    	if(customRegisterCustomerForm != null){
        	requestAttributes.removeAttribute(AuthSuccessController.PREFILLED_CUSTOM_REGISTER_CUSTOMER_FORM, RequestAttributes.SCOPE_SESSION);
        	return customRegisterCustomerForm;
    	}
    	
    	RegisterCustomerForm registerCustomerForm = super.initCustomerRegistrationForm();
    	customRegisterCustomerForm =  new CustomRegisterCustomerForm(registerCustomerForm); 
        return customRegisterCustomerForm;        
    }
}
